package com.esp.studentManagementSystem.service;

import com.esp.studentManagementSystem.pojo.bo.UserBO;
import com.esp.studentManagementSystem.pojo.vo.SecurityGrantedAuthorityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UserDetailService implements UserDetailsService {
    @Autowired
    private UserBOService userBOService;
    @Autowired
    private MenuBOService menuBOService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserBO userBO = userBOService.getSysUserByUsername(username);
        if (userBO == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        List<SecurityGrantedAuthorityVO> authorities = menuBOService.getCodesByUserId(userBO.getUserId());
        userBO.setAuthorities(authorities);
        return userBO;
    }
}
